import { getCompanyID, getPosIDs, getTenantID, getUserDepID, getUserID, requestFunCompon } from '@/utils';
import React from 'react';
import { useRequest } from 'umi';

const useResManageReq = () => {
  const resourceData = useRequest(
    () => {
      let reqUrl = `/resourceController/treeGrid`;
      let bodyParams = {
        // positionID: getPosIDs(),
        userID: getUserID(),
        dicTypeIds: `${getTenantID()}0000`,
        pID: null,
        resourceType: null,
      };
      return requestFunCompon('POST', reqUrl, bodyParams);
    },
    {
      manual: false,
      onSuccess: (res) => {},
    },
  );

  // 处理数据
  let rouTreeData = React.useMemo(() => {
    return resourceData?.data?.obj;
  }, [resourceData.data]);
  return {
    rouTreeData: rouTreeData,
    resourceLoading: resourceData.loading,
    resourceFresh: resourceData.run,
  };
};

// 获取详情
const useResourVierInfo = (bodyParams: any) => {
  const posDetailInfo = useRequest(
    () => {
      let reqUrl = `/resourceController/getResourceByID/${bodyParams?.id}`;
      return requestFunCompon('GET', reqUrl);
    },
    {
      manual: bodyParams && bodyParams?.id ? false : true, //页面初始请求为true
      onSuccess: (res) => {
        if (res && res?.msgFlag) {
          // message.success(res?.msg)
        }
      },
    },
  );

  // 处理数据
  let detailInfoData = React.useMemo(() => {
    return posDetailInfo?.data?.obj;
  }, [posDetailInfo.data]);

  return {
    detailInfoData: detailInfoData,
    infoLoading: posDetailInfo.loading,
    infoRefresh: posDetailInfo.run,
  };
};

// 新增接口 方法
const addEditResoucer = (bodyPayload: any, reqType: string) => {
  let reqUrl = reqType == 'add' ? '/resourceController/add' : reqType == 'edit' ? '/resourceController/edit' : '';
  let methodType = reqType == 'add' ? 'POST' : reqType == 'edit' ? 'PUT' : '';
  if (reqUrl && methodType) {
    return requestFunCompon(methodType, reqUrl, bodyPayload);
  }
};

// 开启关闭 资源状态
const switchResourIsOpen = (bodyPayload: any) => {
  let reqUrl = `/resourceController/modifyState`;
  let methodType = `PUT`;
  if (reqUrl && methodType) {
    return requestFunCompon(methodType, reqUrl, {
      ...bodyPayload,
      modifyerID: getUserID(),
      modifyPositionID: getPosIDs(),
      modifyDepartmentID: getUserDepID(),
      modifyCompanyID: getCompanyID(),
    });
  }
};

export { useResManageReq, useResourVierInfo, addEditResoucer, switchResourIsOpen };
